/**
 * @Author: Boring
 * @Description:
 * @File:  add
 * @Date: 2024-02-25 16:03
 */

package cluster

import (
	"context"
	"github.com/gin-gonic/gin"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"net/http"
	"oam-manage-backend/config"
	"oam-manage-backend/utils/logs"
)

func Delete(r *gin.Context) {
	logs.Deubg(nil, "删除集群")
	// 1.接收删除集群的ID
	clusterId := r.Query("clusterId")
	// 2.删除
	err := config.InClusterClientSet.CoreV1().Secrets(config.MetadataNamespace).Delete(context.TODO(), clusterId, metav1.DeleteOptions{})
	returnData := config.NewReturnData()
	if err != nil {
		logs.Error(map[string]interface{}{"id": clusterId}, "删除集群失败")
		msg := "删除集群失败:" + err.Error()
		returnData.Status = 400
		returnData.Message = msg
	} else {
		logs.Warning(map[string]interface{}{"id": clusterId}, "删除集群成功")
		returnData.Status = 200
		returnData.Message = "删除成功"
		delete(config.ClusterKubeconfig, clusterId)
		//fmt.Println("当前集群配置：", config.ClusterKubeconfig)
	}
	r.JSON(http.StatusOK, returnData)
}
